package com.example.uiclassassignments.Activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

import com.example.uiclassassignments.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.List;

public class LineChartActivity extends Activity {

    private LineChart lineChart;
    private List<Integer> windSpeedList = new ArrayList<Integer>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_chart);
        lineChart = (LineChart) findViewById(R.id.lineChart);
        //lineChart.setDescription("My Description");


        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(12);
        xAxis.setDrawGridLines(false);

        lineChart.getAxisRight().setEnabled(false);//取消右边坐标
        YAxis leftAxis = lineChart.getAxisLeft();//设置左边坐标
        leftAxis.setTextSize(12);
        leftAxis.setAxisMaxValue(10);
        leftAxis.setAxisMinValue(0);
        lineChart.getLegend().setEnabled(false);

        windSpeedList.add(20);
        windSpeedList.add(25);
        windSpeedList.add(26);
        windSpeedList.add(27);
        windSpeedList.add(29);
        windSpeedList.add(20);
        windSpeedList.add(40);
        windSpeedList.add(35);
        setData(windSpeedList);
    }

    public void setData(List<Integer> list) {

        List<String> xValues = new ArrayList<String>();
        List<Entry> yValues = new ArrayList<Entry>();
        for (int i = 0; i < list.size(); i++) {
            xValues.add(i + "点时间");
            yValues.add(new Entry(list.get(i), i));
        }
        LineDataSet dataSet = new LineDataSet(yValues, "Y");
//        dataSet.setValueFormatter(new ValueFormatter() {
//            @Override
//            public String getFormattedValue(float v, Entry entry, int i, ViewPortHandler viewPortHandler) {
//                return (int) v + "风速";
//            }
//        });

        dataSet.setValueTextSize(14);
        dataSet.setValueTextColor(ColorTemplate.getHoloBlue());
        dataSet.setDrawCircleHole(false);
        dataSet.setColor(Color.BLUE);
        dataSet.setCircleSize(4);
        dataSet.setLineWidth(3);

        LineData data = new LineData(dataSet);//这里只有一个参数
        lineChart.setData(data);
        lineChart.invalidate();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
